import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { appContentUrl } from '../../content.url';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { content } from '../../content.data';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { InitService } from '../../../public/serve/init/init.service';

declare var $: any;
@Component({
  selector: 'app-content-product-edit',
  templateUrl: './content-product-edit.component.html'
})
export class ContentProductEditComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();

  details: object = {};

  constructor(
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService,
    private server : Service,
  ) { }

  ngOnInit() {
  }

  /**
   * 详情
   */
  _details(info: object) {
    this.details = info;
  }

  /**
   * 编辑
   * @param info
   */
  _onSubmit(info) {
    info.token = content.retrieval.token;
    info.author = content.retrieval.author;
    this.request
      .postRequest(this.server.baseUrl + appContentUrl.updateProduct, info)
      .then((res: any) => {
        if (res.result_code == "0000") {
          this.onSubmit.emit();
          content.retrieval.author = '';
        }

        this.hintsService.messages(res.message, res.result_code);
        this._init();
        return;
      })
      .catch((err: any) => {
        this.hintsService.messages('error', 'error');
        this._init();
        return;
      });
  }

  /**
   * 初始化
   */
  private _init() {
    this.initService._init();
    $('#contentProductEdit').modal('hide');
    this.onSubmit.emit();
  }

  getAuthorPersons($event) {
    $('#editAssignAuthor').val($event);
    this.onSubmit.emit();
  }

  /**
   * 取消初始化
   */
  _choseInit(){
    content.retrieval.author = '';
    this.onSubmit.emit();
  }

}
